<?php
declare(strict_types=1);

namespace support\bootstrap;

use Illuminate\Database\Capsule\Manager as Capsule;

/**
 * 全局 Eloquent 初始化
 * - 在 Webman 启动时被 config/bootstrap.php 引入
 * - 读取 config/database.php 与 .env，设置为全局并启动 Eloquent
 */
class Database
{
    public static function start(): void
    {
        $config = config('database');
        $default = $config['default'] ?? 'mysql';
        $conn = $config['connections'][$default] ?? $config['connections']['mysql'];

        $capsule = new Capsule();
        $capsule->addConnection([
            'driver'    => $conn['driver'] ?? 'mysql',
            'host'      => $conn['host'] ?? '127.0.0.1',
            'port'      => $conn['port'] ?? 3306,
            'database'  => $conn['database'] ?? '',
            'username'  => $conn['username'] ?? '',
            'password'  => $conn['password'] ?? '',
            'charset'   => $conn['charset'] ?? 'utf8mb4',
            'collation' => $conn['collation'] ?? 'utf8mb4_unicode_ci',
            'prefix'    => $conn['prefix'] ?? '',
            'strict'    => $conn['strict'] ?? false,
        ], 'default');

        $capsule->setAsGlobal();
        $capsule->bootEloquent();
    }
}